<!--
  * 时间：2024/10/25
  * 作者：刘岩
  * 描述：展柜管理
-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属展馆" prop="shopId">
        <el-select
          clearable
          placeholder="请选择所属展馆"
          v-model="queryParams.shopId"
          @change="getHallList(true, queryParams.shopId)"
          filterable
        >
          <el-option
            v-for="item in shopSearchOptions"
            :key="item.shopId"
            :value="item.shopId"
            :label="item.shopName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属展厅" prop="hallId">
        <el-select
          clearable
          placeholder="请先选择所属展馆"
          v-model="queryParams.hallId"
          filterable
        >
          <el-option
            v-for="item in hallSearchOptions"
            :key="item.hallId"
            :value="item.hallId"
            :label="item.hallName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="展柜名称" prop="cabinetName">
        <el-input
          v-model="queryParams.cabinetName"
          placeholder="请输入展柜名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:cabinet:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:cabinet:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:cabinet:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:cabinet:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cabinetList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="展柜ID" align="center" prop="cabinetId" />
<!--      <el-table-column label="租户ID" align="center" prop="tenantId" />-->
<!--      <el-table-column label="展馆ID" align="center" prop="shopId" />-->
      <el-table-column label="所属展厅" align="center" prop="hallName" />
      <el-table-column label="展柜名称" align="center" prop="cabinetName" />
      <el-table-column label="展柜类型" align="center" prop="type" />
      <el-table-column label="展厅状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:cabinet:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:cabinet:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改展柜对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属展馆" prop="shopId">
          <el-select
            clearable
            placeholder="请选择所属展馆"
            v-model="form.shopId"
            filterable
          >
            <el-option
              v-for="item in shopOptions"
              :key="item.shopId"
              :value="item.shopId"
              :label="item.shopName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属展厅" prop="hallId">
          <el-select
            clearable
            placeholder="请先选择所属展馆"
            v-model="form.hallId"
            filterable
          >
            <el-option
              v-for="item in hallOptions"
              :key="item.hallId"
              :value="item.hallId"
              :label="item.hallName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="展柜名称" prop="cabinetName">
          <el-input v-model="form.cabinetName" placeholder="请输入展柜名称" />
        </el-form-item>
        <el-form-item label="展柜状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="展柜类型" prop="type">
          <el-select
            v-model="form.type"
            clearable
            placeholder="请选择展柜类型"
          >
            <el-option
              value="1"
              label="类型1"
            ></el-option>
            <el-option
              value="2"
              label="类型2"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCabinet, getCabinet, delCabinet, addCabinet, updateCabinet } from "@/api/showcaseManage";
import { listHall } from "@/api/exhibitionHallManage";
import {listShop} from "@/api/venueManage";

export default {
  name: "ShowcaseManage",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 展柜表格数据
      cabinetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantId: null,
        shopId: null,
        hallId: null,
        cabinetName: null,
        type: null,
        status: null,
      },
      // 展馆数据
      shopSearchOptions: [],
      shopOptions: [],
      // 展厅数据
      hallOptions: [],
      hallSearchOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        shopId: [
          {required: true, message: '所属展馆不能为空', trigger: 'blur'}
        ],
        hallId: [
          {required: true, message: '所属展厅不能为空', trigger: 'blur'}
        ],
        cabinetName: [
          {required: true, message: '展柜名称不能为空', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '展柜类型不能为空', trigger: 'blur'}
        ]
      }
    };
  },
  watch: {
    // 监听修改时展馆数据改变，请求展厅数据
    'form.shopId' (val) {
      this.getHallList(false, val)
    }
  },
  created() {
    this.getList();
    this.getShopList(true); // 查询展馆数据
  },
  methods: {
    /** 查询展柜列表 */
    getList() {
      this.loading = true;
      listCabinet({
        ...this.queryParams,
        tenantId: this.$store.getters.tenantId
      }).then(response => {
        this.cabinetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /**
     * 时间：2024/10/30
     * 作者：刘岩
     * 功能：查询展馆数据
     * true: 赋值查询模块 否则 走添加修改模块
     */
    getShopList(boolean) {
      listShop({
        pageNum: 1,
        pageSize: 99999,
        tenantId: this.$store.getters.tenantId
      }).then(response => {
        boolean ? this.shopSearchOptions = response.rows : this.shopOptions = response.rows;
      });
    },
    /**
     * 时间：2024/10/30
     * 作者：刘岩
     * 功能：查询展厅列表
     * true: 赋值查询模块 否则 走添加修改模块
     */
    getHallList(boolean, shopId) {
      // 展馆id为空，清空展柜数据
      if (!shopId) {
        boolean ? this.hallSearchOptions = [] : this.hallOptions = [];
        boolean ? this.queryParams.hallId = null : this.form.hallId = null;
        return;
      }
      listHall({
        pageNum: 1,
        pageSize: 99999,
        shopId
      }).then(response => {
        boolean ? this.hallSearchOptions = response.rows : this.hallOptions = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cabinetId: null,
        tenantId: null,
        shopId: null,
        hallId: null,
        cabinetName: null,
        type: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.hallOptions = []; // 重置展厅数据
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cabinetId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.getShopList(false);
      this.title = "添加展柜";
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      await this.reset();
      const cabinetId = row.cabinetId || this.ids
      await this.getShopList(false);
      await getCabinet(cabinetId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改展柜";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cabinetId != null) {
            updateCabinet(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCabinet({
              ...this.form,
              tenantId: this.$store.getters.tenantId
            }).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cabinetIds = row.cabinetId || this.ids;
      this.$modal.confirm('是否确认删除展柜ID为"' + cabinetIds + '"的数据项？').then(function() {
        return delCabinet(cabinetIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/cabinet/export', {
        ...this.queryParams
      }, `展柜_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style scoped lang="scss">
.el-select--medium {
  width: 100%;
}
</style>
